//		ID : Xerxes
//      "I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define tp pair< int, pair<int,int> >
#define mmk( x, y , z ) mk( x, mk( y, z ) )


//********************************************************
const int inf = (int)1e5;
int root[inf+10];
int rank[inf+10];

inline void _Set() {
    fo(i,0,inf) {
        root[i]=i;
        rank[i]=0;
    }
}
inline int _Find(int x) {
    if( x!= root[x] ) {
        root[x] = _Find( root[x] );
    }
    return root[x];
}
inline void _Union(int x, int y) {
    int px = _Find(x);
    int py = _Find(y);
    if( rank[px] > rank[py] ) {
        root[py] = px;
    } else {
        root[px] = py;
    }
    if( rank[px] == rank[py]) {
        rank[py]+=1;
    }
}
//**************************************************************
inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}

struct NODE{
		int x,y,d;
		NODE(int _x, int _y, int _d):
		x(_x),y(_y),d(_d){;}
};
inline bool cmp( NODE A, NODE B){
	return A.d < B.d ;
}

int move[4][2]={ {-1,0},{+1,0},{0,+1},{0,-1} };
char grid[60][60];
int R,C;
vector< pair<int,int> > aliens;
int cost[60][60];
int mex,mey;
vector< NODE > edges;
inline void Print(){
		fo(i,0,R){
			fo(j,0,C){
				printf("%c",grid[i][j]);
			}
			cout<<"\n";
		}
}
inline void Read(){
		CI(C);
		CI(R);
		scanf("\n");
		fo(i,0,R){
			fo(j,0,C){
				scanf("%c",&grid[i][j]);
			}
			scanf("\n");
		}
}
inline void collectAlien(){
	aliens.clear();
	fo(i,0,R){
		fo(j,0,C){
			if( grid[i][j]=='A'){
				aliens.pb( mk(i,j) );
			}
		}
	
	}
}
inline void collectMe(){
	fo(i,0,R){
		fo(j,0,C){
			if( grid[i][j]=='S'){
				mex = i;
				mey = j;
				return ;
			}
		}
	
	}
}
inline void ini(){
	fo(i,0,55){
		fo(j,0,55){
			cost[i][j]=(int)1023456789;
		}
	
	}
}
inline int bfs(int x, int y, int sinkx, int sinky){
		ini();
		queue< pair<int,int> > q;
		q.push( mk(x,y) );
		cost[x][y]=0;
		while( q.empty()==false ){
			x = q.front().f;
			y = q.front().s;
			q.pop();
			fo(i,0,4){
				int tr = x+move[i][0];
				int tc = y+move[i][1];
				if( tr < 0 || tc < 0 || tr >=R || tc>=C )continue;
				if( grid[tr][tc]=='#')continue;
				if( cost[x][y]+1 < cost[tr][tc] ){
					q.push( mk(tr,tc) );
					cost[tr][tc] = cost[x][y]+1;
				}
			}
		}
		
	return cost[sinkx][sinky];
}
inline void metoAlien(){
	for(int i=0,j=SZ(aliens); i<j; ++i){
		int d = bfs(mex,mey, aliens[i].f, aliens[i].s);
		edges.pb( NODE(0,i+1, d) );
	}
}
inline void alientoAlien(){
	for(int i=0,j=SZ(aliens); i<j; ++i){
		fo(k,i+1,j){
			int d = bfs( aliens[i].f, aliens[i].s, aliens[k].f, aliens[k].s);
			edges.pb( NODE(i+1,k+1, d) );
		}
	}
}

inline void MST(){
		
		sort(edges.begin(),edges.end(), cmp);
		_Set();
		int nodes = SZ(aliens)+1;
		
		int ccost=0;
		for(int i=0,j=SZ(edges),k=0; i<j && k!=(nodes-1); ++i){
			int x = edges[i].x;
			int y = edges[i].y;
			int d = edges[i].d;
			if(_Find(x)!=_Find(y)){
				_Union(x,y);
				ccost+=d;
				++k;
			}
		}
		cout<<ccost<<"\n";
		
		
}
inline void Proc(){
	edges.clear();
	
	collectAlien();
	collectMe();
	metoAlien();
	alientoAlien();
	
	MST();
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in","rt",stdin);
    #endif
    int cases=1;
    for( CI(cases) ; cases ; --cases ){
        Read();
        Proc();
    }

return 0;
}


